package com.tangkun.environment.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.baidu.mapapi.search.sug.SuggestionResult;
import com.tangkun.environment.R;
import com.tangkun.environment.activity.ZiXunDetailActivity;
import com.tangkun.environment.entity.xinwen.QuestionReturn;
import com.tangkun.environment.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author tangkun
 * @ClassName: LocationDemo3Adapter
 * @Description: 详细地址搜索列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class LocationDemo3Adapter extends BaseAdapter implements Filterable {

    private ArrayFilter mFilter;
    private ArrayList<SuggestionResult.SuggestionInfo> mUnfilteredData;
    private Context context;
    private List<SuggestionResult.SuggestionInfo> listData = new ArrayList<SuggestionResult.SuggestionInfo>();

    /**
     * @param context
     * @param listData
     */
    public LocationDemo3Adapter(Context context, List<SuggestionResult.SuggestionInfo> listData) {
        this.context = context;
        this.listData = listData;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int i) {
        return listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_locationdemo3, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (listData != null) {
            // 内容
            if (!TextUtils.isEmpty(listData.get(position).key)) {
                holder.tvContent.setText(listData.get(position).key);
            } else {
                holder.tvContent.setText("");
            }
        }
        return convertView;

    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    private class ArrayFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            if (mUnfilteredData == null) {
                mUnfilteredData = new ArrayList<SuggestionResult.SuggestionInfo>(listData);
            }

            if (prefix == null || prefix.length() == 0) {
                ArrayList<SuggestionResult.SuggestionInfo> list = mUnfilteredData;
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();

                ArrayList<SuggestionResult.SuggestionInfo> unfilteredValues = mUnfilteredData;
                int count = unfilteredValues.size();

                ArrayList<SuggestionResult.SuggestionInfo> newValues = new ArrayList<SuggestionResult.SuggestionInfo>(count);

                for (int i = 0; i < count; i++) {
                    SuggestionResult.SuggestionInfo pc = unfilteredValues.get(i);
                    if (pc != null) {

                        if (pc.key != null && pc.key.startsWith(prefixString)) {
                            newValues.add(pc);
                        }
                    }
                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            //noinspection unchecked
            listData = (List<SuggestionResult.SuggestionInfo>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    }

    static class ViewHolder {
        @BindView(R.id.tv_content)
        TextView tvContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
